// pages/startCheck/startCheck.js
const app = getApp()
const env = app.globalData.env
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  searchQueue() {
    let _this = this
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/searchQueue' : 'https://www.wantstong.com/api/searchQueue',
      method: 'POST',
      data: _this.data.checkData,
      success: res => {
        if (res.data.msg == "have") {
          _this.setData({
            checkPerson: res.data.checkData
          })
        } else {
          _this.setData({
            checkPerson: null
          })
        }

      }
    })
  },

  check() {
    let _this = this
    wx.request({
      url: env != 1 ? 'http://localhost:88/api/check' : 'https://www.wantstong.com/api/check',
      method: 'POST',
      data: _this.data.checkData,
      success: res => {
        _this.setData({
          gradeMember: res.data.data
        })
      }
    })

  },

  formSubmit(e) {
    let _this = this
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    let detailValue = e.detail.value
    let memberData = []
    for (let i in detailValue) {
      let data = {}
      data.gradeId = i
      data.value = detailValue[i]
      data.userId = _this.data.user.userId
      data.checkerName = _this.data.user.nickName

      data.courseId = _this.data.checkData.courseId
      data.taskId = _this.data.checkData.taskId,
        data.finishTime = util.getDatetime(),
        memberData.push(data)
    }
    let trueFlag = 0

    for (let i = 0; i < memberData.length; i++) {
      console.log(memberData[i].value,_this.data.gradeMember[i].gradeValue)
      if (memberData[i].value > _this.data.gradeMember[i].gradeValue) {
        wx.showToast({
          title: '成绩不能大于预期分值',
          icon:'none'
        })

      } else {
        trueFlag++
        _this.setData({
          trueFlag: trueFlag
        })
      }

    }

    if (_this.data.trueFlag == memberData.length) {
      wx.request({
        url: env != 1 ? 'http://localhost:88/api/recordGrade' : 'https://www.wantstong.com/api/recordGrade',
        method: 'POST',
        data: {
          memberData: memberData,
          checkPerson: _this.data.checkPerson
        },
        success: res => {
          if (res.data.msg == "success") {
            wx.showToast({
              title: '记录完成',
            })
            e.detail.value = null
            _this.searchQueue()

          }
        }

      })
    }


  },

  getUser() {
    let _this = this
    wx.getStorage({
      key: 'user',
      success: (res) => {
        console.log("getUser", res)
        _this.setData({
          user: res.data
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this

    wx.getStorage({
      key: 'checkItem',
      success:res=>{
        console.log("checkItem",res)
        _this.setData({
          checkData: res.data
        })
        this.check()
        this.searchQueue()
        this.getUser()
      }
    })
    console.log("/pages/startCheck/startCheck")
    // const eventChannel = this.getOpenerEventChannel()
    // eventChannel.on('startCheckData', function (data) {
    //   console.log("startCheckData", data.data)
    //   _this.setData({
    //     checkData: data.data
    //   })
    // })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})